﻿Public Class Frm_Avoc_Clien

    Dim bol As Boolean = True
    Public avo As Avocat_partenaire
    Dim ra9m As Integer
    Dim faragh As String

    Private Sub Btn_ajou_fax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_fax.Click
        Partager.isFaxClicked = True
        Me.Enabled = False
        Frm_Fax_Bure.txt_tele.Select()

        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Fax_Bure.List_tele.Items.Clear()
                Partager.list_fax.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Fax" Then
                        Frm_Fax_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
        Frm_Fax_Bure.Show()
    End Sub

    Private Sub Btn_parc_ville_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_ville.Click
        Me.Enabled = False
        Frm_Avoc_Bure_ville.txt_ville.Select()
        Frm_Avoc_Bure_ville.Show()
    End Sub

    Private Sub Btn_parc_hay2a_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_hay2a.Click
        Me.Enabled = False
        Frm_Avoc_Bure_hay2a.txt_hay2a.Select()
        Frm_Avoc_Bure_hay2a.Show()
    End Sub

    Private Sub Btn_ajou_tele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele.Click
        Partager.typePhone = "Tel"
        Partager.isTelClicked = True
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()

        '------
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_hatif.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_ajou_tele_tabit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele_tabit.Click
        Partager.typePhone = "Fix"
        Partager.isFixClicked = True
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_fix.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Fix" Then
                        Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Me.Enabled = False
        Frm_Rech_Avoc_Clien.Show()
    End Sub

    Private Sub Frm_Avoc_Clien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_3a2ili.Select()
        Try
            Partager.moi.remplir_List_avoca_partenaire()
            List_tous.Items.Clear()
            For Each khib As Avocat_partenaire In Partager.moi.List_avoca_partenaire
                List_tous.Items.Add(New ListViewItem(New String() {khib.Nom_a, khib.Prenom_a, khib.Num_Prof, khib.Ville, khib.Autorite, khib.Email, khib.Adress}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Partager.com_ville(comb_ville, "Comb_Ville", "Ville")
        Partager.com_ville(Comb_hay2a, "Comb_hay2a", "hay2a")
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            Partager.close()
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة محام جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                If txt_mail.Text <> "" Then
                    bol = Partager.regEmail(txt_mail.Text)
                    If (bol = False) Then
                        txt_mail.Text = "البريد الإلكتروني خاطئ"
                        txt_mail.ForeColor = Color.Red
                        txt_mail.TextAlign = HorizontalAlignment.Right
                    End If
                End If

                For Each av As Avocat_partenaire In Partager.moi.List_avoca_partenaire
                    If txt_ra9_mihani.Text <> "" Then
                        If IsNumeric(txt_ra9_mihani.Text) Then
                            If av.Num_Prof = txt_ra9_mihani.Text Then
                                MsgBox("الرقم المهني استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                                Exit Sub
                            End If
                        Else
                            MsgBox("خطأ في الرقم المهني", MsgBoxStyle.Critical, "خطأ")
                            Exit Sub
                        End If
                    End If
                Next

                If txt_ra9_mihani.Text <> "" Then
                    If Not IsNumeric(txt_ra9_mihani.Text) Then
                        MsgBox("خطأ في الرقم المهني", MsgBoxStyle.Critical, "خطأ")
                        Exit Sub
                    End If
                End If

                If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "" And bol = True) Then
                    If MsgBox("هل تريد فعلا إضافة هذا المحامي ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة محامين الخصم") = MsgBoxResult.Yes Then
                        Try

                            Partager.ControleNull(Me)
                            Dim cli As New Avocat_partenaire(1, txt_ra9_mihani.Text, txt_nom.Text, txt_prenom.Text, txt_3a2ili.Text, txt_chakhssi.Text, comb_ville.Text, Comb_hay2a.Text, txt_adresse.Text, txt_mail.Text, txt_molahadat.Text)
                            Partager.moi.Ajouter_avoca_partenaire(cli)

                            'l'objet en cours
                            Dim lastItem As Integer = Partager.moi.List_avoca_partenaire.Count - 1
                            cli = CType(Partager.moi.List_avoca_partenaire.Item(lastItem), Avocat_partenaire)

                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next
                            For Each d As Phone In Partager.list_fix
                                cli.Ajouter_phone(d)
                            Next
                            For Each d As Phone In Partager.list_fax
                                cli.Ajouter_phone(d)
                            Next

                            List_tous.Items.Clear()
                            For Each khib As Avocat_partenaire In Partager.moi.List_avoca_partenaire
                                List_tous.Items.Add(New ListViewItem(New String() {khib.Nom_a, khib.Prenom_a, khib.Num_Prof, khib.Ville, khib.Autorite, khib.Email, khib.Adress}))
                            Next
                            MsgBox("تمت إضافة المحامي بنجاح", MsgBoxStyle.Information, "نجاح")
                            Partager.vider(Me)
                        Catch ex As Exception

                            MsgBox(ex.Message)

                        End Try
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            End If
        Catch ex As Exception

            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If List_tous.SelectedIndices.Count > 0 Then

            If txt_mail.Text <> "" Then
                bol = Partager.regEmail(txt_mail.Text)
                If (bol = False) Then
                    txt_mail.Text = "البريد الإلكتروني خاطئ"
                    txt_mail.ForeColor = Color.Red
                    txt_mail.TextAlign = HorizontalAlignment.Right
                End If
            End If

            For Each av As Avocat_partenaire In Partager.moi.List_avoca_partenaire
                If txt_ra9_mihani.Text <> "" Then
                    If IsNumeric(txt_ra9_mihani.Text) Then
                        If avo.Num_Prof <> av.Num_Prof Then
                            If av.Num_Prof = txt_ra9_mihani.Text Then
                                MsgBox("الرقم المهني استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                                Exit Sub
                            End If
                        End If
                    Else
                        MsgBox("خطأ في الرقم المهني", MsgBoxStyle.Critical, "خطأ")
                        Exit Sub
                    End If
                End If
            Next

            If txt_ra9_mihani.Text <> "" Then
                If Not IsNumeric(txt_ra9_mihani.Text) Then
                    MsgBox("خطأ في الرقم المهني", MsgBoxStyle.Critical, "خطأ")
                    Exit Sub
                End If
            End If

            If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "" And bol = True) Then
                If MsgBox("هل تريد فعلا تعديل هذا المحامي ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل محامين الموكل") = MsgBoxResult.Yes Then

                    Try
                        Partager.ControleNull(Me)
                        Dim cli As New Avocat_partenaire(1, txt_ra9_mihani.Text, txt_nom.Text, txt_prenom.Text, txt_3a2ili.Text, txt_chakhssi.Text, comb_ville.Text, Comb_hay2a.Text, txt_adresse.Text, txt_mail.Text, txt_molahadat.Text)
                        Partager.moi.modifier_avoca_partenaire(cli, avo.Id)
                        cli.Id = avo.Id
                        If Partager.list_hatif.Count > 0 Or Partager.isTelClicked Then
                            cli.suprimmer_phone(avo.Id, "Tel")
                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next
                        End If
                        If Partager.list_fix.Count > 0 Or Partager.isFixClicked Then
                            cli.suprimmer_phone(avo.Id, "Fix")
                            For Each d As Phone In Partager.list_fix
                                cli.Ajouter_phone(d)
                            Next
                        End If
                        If Partager.list_fax.Count > 0 Or Partager.isFaxClicked Then
                            cli.suprimmer_phone(avo.Id, "Fax")
                            For Each d As Phone In Partager.list_fax
                                cli.Ajouter_phone(d)
                            Next
                        End If

                        List_tous.Items.Clear()
                        For Each khib As Avocat_partenaire In Partager.moi.List_avoca_partenaire
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Nom_a, khib.Prenom_a, khib.Num_Prof, khib.Ville, khib.Autorite, khib.Email, khib.Adress}))
                        Next
                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me)
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Else
            MsgBox("اختر محام من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Partager.close()
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا المحامي ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف محامين الخصم") = MsgBoxResult.Yes Then
                Try
                    Dim clt As Avocat_partenaire = Partager.moi.List_avoca_partenaire(List_tous.SelectedIndices.Item(0))
                    Partager.moi.suprimmer_avoca_partenaire(avo.Id)
                    List_tous.Items.Clear()
                    For Each khib As Avocat_partenaire In Partager.moi.List_avoca_partenaire
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Nom_a, khib.Prenom_a, khib.Num_Prof, khib.Ville, khib.Autorite, khib.Email, khib.Adress}))
                    Next
                    MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر محام من القائمة", MsgBoxStyle.Information, "! خـطأ")
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices
                avo = Partager.moi.List_avoca_partenaire(i)
                txt_ra9_mihani.Text = avo.Num_prof
                txt_3a2ili.Text = avo.Nom_a
                txt_chakhssi.Text = avo.Prenom_a
                txt_nom.Text = avo.Nom
                txt_prenom.Text = avo.Prenom
                txt_adresse.Text = avo.Adress
                txt_mail.Text = avo.Email
                comb_ville.Text = avo.Ville
                Comb_hay2a.Text = avo.Autorite
                txt_molahadat.Text = avo.Remarque

                avo.remplir_List_list_phone()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Partager.list_hatif.Add(numhone)
                    ElseIf numhone.Type = "Fix" Then
                        Partager.list_fix.Add(numhone)
                    ElseIf numhone.Type = "Fax" Then
                        Partager.list_fax.Add(numhone)
                    End If
                Next
            Next
            If List_tous.SelectedIndices.Count <= 0 Then
                Partager.vider(Me)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        txt_3a2ili.Select()
    End Sub

    Private Sub txt_mail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_mail.Click
        If bol = False Then
            txt_mail.Text = ""
        End If
        bol = True
    End Sub

    Private Sub txt_mail_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mail.KeyPress
        If e.KeyChar = ChrW(32) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txt_mail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_mail.TextChanged
        txt_mail.ForeColor = Color.Black
        txt_mail.TextAlign = HorizontalAlignment.Left
    End Sub

    Private Sub txt_ra9_mihani_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_ra9_mihani.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub txt_ra9_mihani_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_ra9_mihani.TextChanged

    End Sub
End Class